<script setup>
import {useScoreStore, useUserStore} from "@/store/userStore.js";
import {onMounted, ref} from "vue";
import addUser from "@/components/addUser.vue";
import {useToast} from "vue-toastification";
import {addUserClass, getAllClass, getUserByName} from "@/hook/userHook.js";

let user = ref({
  username: "",
  password: "",
  realName: "",
  email: "",
  phone: "",
  gender: 1,
  role:"",
  class:'',
})
let classes = ref([])
onMounted(async () => {
  classes.value = await getAllClass();
})

let onSubmit=(async () => {
  if (useUserStore().search_type === '学号') {
    user.value.role = 'STUDENT'
  } else if (useUserStore().search_type === '教师号') {
    user.value.role = 'TEACHER'
  } else if (useUserStore().search_type === '管理员') {
    user.value.role = 'ADMIN'
  }
  user.value.gender = user.value.gender === true ? 2 : 1;
  console.log(user.value)
  //添加新用户到数据库
  let flag = await useUserStore().addUser(user.value);
  if(useUserStore().search_type === '管理员'){
    useUserStore().updateAdmins(1,5)
    return
  }

  //如果添加成功就执行下一步
  if(flag){
    //查询刚才添加的用户id
    let userId = await (await getUserByName(user.value.username)).id;
    let courseId = classes.value.filter(item=>item.name===user.value.class)[0].id;
    console.log(userId,user.value.class,"查询到的用户id,和要绑定添加的班级id");
    //根据用户id和课程id,添加关联信息到数据库表:user_class
    await addUserClass(userId,courseId)
    if (useUserStore().search_type === '学号') {
      useUserStore().updateStudents(1,5)
    } else if (useUserStore().search_type === '教师号') {
      useUserStore().updateTeachers(1,5)
    }
  }

})

</script>

<template>
  <div class="bg-base-100 border-base-300 collapse border text-base-content">
    <input type="checkbox" class="peer"/>
    <div class="btn btn-dash btn-success bi bi-person-add collapse-title text-base-content">
      新增用户
    </div>
    <div class="border-dashed border py-3 my-3 border-success border-t-none collapse-content text-base-content">
      <form class="grid grid-cols-3  gap-2" @submit.prevent="onSubmit">
        <div>
          <input type="text" class="input validator" required :placeholder="useUserStore().search_type === '教师号'?'教师号':'学号'"
                 pattern="[A-Za-z0-9\-]*" minlength="3" maxlength="10" title="学号"
              v-model="user.username"/>
          <p class="validator-hint">必须是3~10个字符</p>
        </div>
        <div>
          <input type="password" class="input validator" required placeholder="密码" minlength="8"
                 pattern="[A-Za-z/d]).{8,}"
                 title="密码" v-model="user.password"/>
          <p class="validator-hint">
            包括 8 个字符
          </p>
        </div>
        <div>
          <input type="text" class="input validator text-base-content" required placeholder="姓名"
                 minlength="2" maxlength="30" title="姓名"
          v-model="user.realName"/>
          <p class="validator-hint">请正确填写姓名</p>
        </div>
        <!--权限-->
        <div>
          <input class="input validator text-base-content" type="email" required placeholder="xx@xx.com"
                 v-model="user.email" />
          <div class="validator-hint">请输入正确的邮箱地址</div>
        </div>
        <div>
          <input type="tel" class="input validator tabular-nums text-base-content" required placeholder="手机号码"
                 pattern="[0-9]*" minlength="3" maxlength="11" title="手机号码"
                 v-model="user.phone"/>
          <p class="validator-hint">3~11位手机号码</p>
        </div>
        <div>
          男 <input type="checkbox" checked="checked" class="toggle" v-model="user.gender"/> 女
        </div>
        <div v-if="useUserStore().search_type !== '管理员'">
          <select class="select select-ghost" v-model="user.class">
            <option v-for="(c) in classes" :key="c.id">{{ c.name }}</option>
          </select>
        </div>
        <div class="flex gap-4 justify-center  ">
          <button class="btn  flex-1" type="submit">提交</button>
          <button class="btn btn-dash flex-1" type="reset">重置</button>
        </div>
      </form>
    </div>
  </div>
</template>

<style scoped>

</style>